package com.nulldev.util.web.YouTubeExtractor.testing.tests;

import com.nulldev.util.internal.testing.Test;
import com.nulldev.util.internal.testing.TestResult;
import com.nulldev.util.web.YouTubeExtractor.signatures.ExtractedSignatureData;
import com.nulldev.util.web.YouTubeExtractor.signatures.extractor.V9Extractor;
import com.nulldev.util.web.YouTubeExtractor.signatures.extractors.v9.NashornBasedV9Extractor;

public class DeciphererV9Tests {

	private static final String payload_v1 = "gha=function(a){var b=a.split(\"\"),c=[-1993858242,1331704449,null,2050541470,function(d,e){for(var f=64,h=[];++f-h.length-32;)switch(f){case 94:case 95:case 96:break;case 123:f-=76;case 92:case 93:continue;case 58:f=44;case 91:break;case 46:f=95;default:h.push(String.fromCharCode(f))}d.forEach(function(l,m,n){this.push(n[m]=h[(h.indexOf(l)-h.indexOf(this[m])+m-32+f--)%h.length])},e.split(\"\"))},"
			+ "-1835249762,b,1300417474,function(d,e){e=(e%d.length+d.length)%d.length;d.splice(-e).reverse().forEach(function(f){d.unshift(f)})},"
			+ "function(d){d.reverse()},"
			+ "1361640375,-1923658975,-2131942794,1888365334,null,1867567764,function(d,e){e=(e%d.length+d.length)%d.length;d.splice(-e).reverse().forEach(function(f){d.unshift(f)})},"
			+ "b,-1472917265,function(d,e){e=(e%d.length+d.length)%d.length;d.splice(0,1,d.splice(e,1,d[0])[0])},"
			+ "1882880937,2022955584,1544155301,function(d,e){for(var f=64,h=[];++f-h.length-32;){switch(f){case 58:f-=14;case 91:case 92:case 93:continue;case 123:f=47;case 94:case 95:case 96:continue;case 46:f=95}h.push(String.fromCharCode(f))}d.forEach(function(l,m,n){this.push(n[m]=h[(h.indexOf(l)-h.indexOf(this[m])+m-32+f--)%h.length])},e.split(\"\"))},"
			+ "-2050439670,883390298,181540238,1780769224,-196999404,1792168217,\"debugger\",193622632,-215551247,function(d,e){e=(e%d.length+d.length)%d.length;var f=d[0];d[0]=d[e];d[e]=f},"
			+ "-418218955,-1667824018,function(d,e){for(e=(e%d.length+d.length)%d.length;e--;)d.unshift(d.pop())},"
			+ "-1820659387,function(d){for(var e=d.length;e;)d.push(d.splice(--e,1)[0])}," + "-891599941,null,1682428368,\"BNa9\",function(d,e){d.push(e)},"
			+ "function(d,e){e=(e%d.length+d.length)%d.length;d.splice(e,1)},"
			+ "553061301,1819926594,b,2022955584];c[2]=c;c[14]=c;c[40]=c;try{c[9](c[17]),c[36](c[14],c[1]),c[44](c[38],c[21]),c[35](c[46],c[40]),c[35](c[38],c[9]),c[34](c[5],c[0]),c[10](c[38],c[11]),c[29](c[5]),c[22](c[7],c[17]),c[38](c[36]),c[11](c[13]),c[40](c[21],c[31]),c[40](c[9],c[42]),c[32](c[9],c[49]),c[18](c[36]),c[9](c[28],c[35]),c[6](c[20],c[45]),c[42](c[24],c[33]),c[6](c[37],c[34]),c[6](c[24],c[8]),c[42](c[24],c[4]),c[39](c[24],c[10]),c[43](c[41],c[18]),c[5](c[3],c[11]),c[19](c[0],c[34]),c[30](c[33],"
			+ "c[40]),c[9](c[41],c[28]),c[19](c[37],c[42]),c[22](c[26],c[32]),c[13](c[35],c[19]),c[49](c[31],c[32]),c[13](c[27],c[9]),c[44](c[35]),c[49](c[31],c[45]),c[37](c[27],c[43]),c[13](c[27],c[6]),c[46](c[31],c[8]),c[45](c[10],c[0]),c[19](c[24],c[49])}catch(d){console.error(d);return\"enhanced_except_gJMB_-z-_w8_\"+a}return b.join(\"\")};";
	private static final String input_v1 = "TxrJXs9V6hYCRR2Alr1xj";
	private static final String output_v1 = "Is-qw23L0WjNVP9Q8";

	private static final String payload_v2 = "function Yke(a){var b=a.split(\"\"),c=[1070485609,7,function(d,e){d.push(e)},\n"
			+ "1820898764,b,724822213,-1931199752,-480619754,-940740727,function(d,e){d=(d%e.length+e.length)%e.length;var f=e[0];e[0]=e[d];e[d]=f},\n"
			+ "102350667,function(d,e,f,h,l,m,n){return d(l,m,n)},\n"
			+ "function(){for(var d=64,e=[];++d-e.length-32;){switch(d){case 58:d-=14;case 91:case 92:case 93:continue;case 123:d=47;case 94:case 95:case 96:continue;case 46:d=95}e.push(String.fromCharCode(d))}return e},\n"
			+ "-1014164158,null,165635886,'(\"/',function(d,e,f,h,l,m){return e(h,l,m)},\n"
			+ "function(d,e){for(d=(d%e.length+e.length)%e.length;d--;)e.unshift(e.pop())},\n"
			+ "function(){for(var d=64,e=[];++d-e.length-32;)switch(d){case 58:d=96;continue;case 91:d=44;break;case 65:d=47;continue;case 46:d=153;case 123:d-=58;default:e.push(String.fromCharCode(d))}return e},\n"
			+ "function(d,e){d=(d%e.length+e.length)%e.length;e.splice(d,1)},\n"
			+ "552875556,-1711140149,-343092183,function(d,e,f,h,l,m,n,p,q){return f(h,l,m,n,p,q)},\n"
			+ "730350776,-1514153288,1729308886,6,399121607,-362793188,function(d,e,f,h,l,m,n,p){return e(f,h,l,m,n,p)},\n"
			+ "395619571,function(d,e){d=(d%e.length+e.length)%e.length;e.splice(0,1,e.splice(d,1,e[0])[0])},\n"
			+ "9,-1014164158,-1286739758,796553404,-2049778790,8,function(d){d.reverse()},\n" + "-2052212221,\"ciLm\",function(d,e){e.splice(e.length,0,d)},\n"
			+ "1166934418,function(){for(var d=64,e=[];++d-e.length-32;){switch(d){case 91:d=44;continue;case 123:d=65;break;case 65:d-=18;continue;case 58:d=96;continue;case 46:d=95}e.push(String.fromCharCode(d))}return e},\n"
			+ "\"TzAjJ\",null,-882637207,function(d,e,f,h,l,m,n,p,q,v){return e(l,m,n,p,q,v)},\n"
			+ "\"SYY78\",644117838,function(d){for(var e=d.length;e;)d.push(d.splice(--e,1)[0])},\n"
			+ "872846529,-1310736936,-1687214745,-1082845239,-105270901,969766314,1012316493,\"zOtkrOj\",1,-206240569,4,423285034,1533806340,function(d,e,f){var h=f.length;d.forEach(function(l,m,n){this.push(n[m]=f[(f.indexOf(l)-f.indexOf(this[m])+m+h--)%f.length])},e.split(\"\"))},\n"
			+ "-800822164,-283180137,430088225,5,function(d,e){e=(e%d.length+d.length)%d.length;d.splice(-e).reverse().forEach(function(f){d.unshift(f)})},\n"
			+ "-1524657126,-687426532,730350776,1647588555,-534194751,\"6l60H\",\"1mxeKy4\",\"d5XNn\",\"wgysiSF\",-1420495796,-1440779358,836131858,\"eq8y5\",722308474,0,1294718932,function(){for(var d=64,e=[];++d-e.length-32;)switch(d){case 46:d=95;default:e.push(String.fromCharCode(d));case 94:case 95:case 96:break;case 123:d-=76;case 92:case 93:continue;case 58:d=44;case 91:}return e},\n"
			+ "-1204602844,-800822164,3,b,-1687214745,1255402076,function(d,e,f,h,l){return e(f,h,l)},\n"
			+ "b,\"6ThS_\",\"PST1Cc\",1839762627,1988279585,53310805,\"vE8tYx-\",null,-1827961539,\"\\\\\",359164845,-357216702,-1286739758];c[14]=c;c[47]=c;c[103]=c;try{try{4<c[28]&&(6<c[new Date(\"December 31 1969 20:00:01 EDT\")/1E3]?(0,c[17])((((0,c[new Date(\"January 01 1970 08:30:40 +0830\")/1E3])(c[103]),c[68])(c[16]),c[90])(c[98],c[61]),c[84],(0,c[84])(c[77],c[6]),c[54],c[new Date(\"December 31 1969 20:01:48 EDT\")/1E3]):(0,c[79])((0,c[79])((0,c[Math.pow(4,1)+-18012+18084])(c[58],c[92]),c[56],(0,c[76])(c[28],c[4]),c[4]),"
			+ "c[76],(0,c[30])(c[92],c[54],(0,c[8])()),c[45],c[4])),9>=c[62-Math.pow(3,3)%497]&&(c[33]>30*Math.pow(2,1)-53||((0,c[44])(c[0]),void 0))&&(0,c[56])(c[0]),1<c[57]&&(3<c[62]||((0,c[72])((0,c[76])(c[69],c[92]),(0,c[1])((0,c[78])(c[14],c[82]),c[30],c[92],c[19],(0,c[new Date(\"1969-12-31T20:00:08.000-04:00\")/1E3])()),c[1],(0,c[44])(c[102]),c[76],c[64],c[0]),0))&&(0,c[47])((0,c[63])(c[23],c[82]),c[1],(0,c[new Date(\"12/31/1969 20:01:18 EDT\")/1E3])(c[106],c[4]),(0,c[78])(c[42],c[92]),(0,c[63])(c[67],c[62]),"
			+ "c[78],c[81],c[Math.pow(8,1)+-9576- -9660])}catch(d){((0,c[30])(c[4],c[36],(0,c[84])()),c[76])(c[31],c[102]),(0,c[30])(c[62],c[108],(0,c[84])())}finally{3<c[26]&&(0>=c[10]||((0,c[1])((0,c[76])(c[98],c[102]),c[30],c[new Date(\"31 December 1969 18:01:02 CST\")/1E3],c[18],(0,c[51])()),NaN))&&(0,c[1])((0,c[78])(c[21],c[102]),c[61],c[40],c[70]),1<c[93]&&(0>=c[41]||((0,c[37])((0,c[37])((0,c[37])((0,c[14])(c[32],c[85]),c[89],c[25],c[9]),c[99],c[43],c[29]),c[99],c[91],c[40]),0))&&(0,c[0])((0,c[37])((0,c[99])(c[17],"
			+ "c[29]),c[14],c[1],c[98]),c[66],(0,c[66])(c[15+Math.pow(new Date(\"Wednesday December 31 1969 20:00:04 EDT\")/1E3,1)%23],c[48],(0,c[4])()),c[98],c[86],(0,c[8030-Math.pow(5,5)-4894])()),3<c[93]&&(4>=c[71]&&((0,c[37])((0,c[83])((0,c[0])((0,c[14])(c[28],c[98]),c[14],(0,c[37])((0,c[new Date(\"01 January 1970 02:16:06 +0215\")/1E3])(c[40],c[82],(0,c[44])()),c[5],c[31],c[9]),c[14],c[84]),c[75],((0,c[80])(c[28],c[63]),(0,c[26])(c[104],c[16]),c[48])(c[37],c[68]),(0,c[65])(c[10],(0,c[48])(c[-1630175+97*Math.pow(7,"
			+ "5)],c[0]),(0,c[94])(c[75],c[99]),(0,c[73])(c[17],c[47]),c[88],c[25]),(0,c[366%Math.pow(7,3)- -82])(c[14],c[109]),c[87],(0,c[52])(c[23]),c[105],c[new Date(\"01 January 1970 00:00:12 GMT\")/1E3]),c[103],c[new Date(\"01/01/1970 00:00:01 UTC\")/1E3],c[36]),\"1\")||((0,c[9])((0,c[52])(c[12]),c[38],c[70],c[25],(0,c[16])()),(((((0,c[96])(c[78],c[23]),c[9])((((0,c[64])(c[1]),c[71])(c[74],c[91]),c[64])(c[91]),c[87],c[11],c[57]),c[9])((0,c[87])(c[19],c[91]),c[Math.pow(6,4)+1120-2352],c[12]),c[7])(c[102],c[23]),(0,c[87])(c[84],"
			+ "c[91]),c[96])(c[32],c[12]),c[82])((0,c[38])(c[70],c[2],(0,c[59])()),c[96],(0,c[7])(c[10],c[23]),c[45],c[1]))}try{4<c[76]&&(4<c[8]&&((0,c[9])((0,c[new Date(\"12/31/1969 16:00:52 -0800\")/1E3])(c[12]),c[71],c[92],c[1]),\",\")||(0,c[9])((0,c[33])(c[70],c[51]),c[240*Math.pow(7,3)+-82287],c[57],c[46])),5<c[65]&&(0,c[9])((0,c[64])(c[12]),c[96],c[100],c[91]),1<c[76]&&(2>=c[34]&&((0,c[71-Math.pow(6,2)+38])((0,c[9])((0,c[33])(c[1],c[14]),c[7],c[0],c[23]),c[9],(0,c[52])(c[23]),c[38],c[23],c[6],(0,c[86])()),1)||"
			+ "(((0,c[9])((0,c[96])(c[67],c[57]),c[33],c[57],c[47]),c[32])(c[42],c[68]),c[9])(c[79]))}catch(d){(0,c[94])(c[79],c[80],(0,c[31])()),(0,c[63])(c[28+376%Math.pow(3,3)],c[15]),(0,c[230*Math.pow(2,3)+-1777])(c[84],c[68])}}catch(d){return\"enhanced_except_jpYB8en-_w8_\"+a}return b.join(\"\")};";

	public static final ExtractedSignatureData ESD = new ExtractedSignatureData(payload_v1, payload_v1, "gha");
	public static final ExtractedSignatureData ESD2 = new ExtractedSignatureData(payload_v2, payload_v2, "Yke");

	public static class V9DeciphererTest extends Test<String> {

		private final V9Extractor ex;

		public V9DeciphererTest(final V9Extractor ex) {
			super(output_v1);
			this.ex = ex;
		}

		public V9DeciphererTest(final TestResult ex) {
			super(ex);
			this.ex = null;
		}

		@Override
		public void test() throws Throwable {
			this.str_finish(ex.decrypt(input_v1));
		}

	}


//	public static class V9DeciphererBaseJSTest extends V9DeciphererTest {
//		
//		public V9DeciphererBaseJSTest() {
//			super(new BaseJSExtractor(ESD));
//		}
//		
//	}

	public static class V9DeciphererNashhornTest extends V9DeciphererTest {

		public V9DeciphererNashhornTest() {
			super(new NashornBasedV9Extractor(ESD));
		}

		public V9DeciphererNashhornTest(final boolean noNashorn) {
			super(TestResult.DISABLED);
		}

	}

//	public static class V9DeciphererRhinoTest extends V9DeciphererTest {
//		
//		public V9DeciphererRhinoTest() {
//			super(new RhinoBasedExtractor(ESD));
//		}
//		
//		public V9DeciphererRhinoTest(final boolean noNashorn) {
//			super(TestResult.DISABLED);
//		}
//		
//	}

//	public static class V9DeciphererGraalTest extends V9DeciphererTest {
//		
//		public V9DeciphererGraalTest() {
//			super(new GraalBasedExtractor(ESD));
//		}
//		
//		public V9DeciphererGraalTest(final boolean noNashorn) {
//			super(TestResult.DISABLED);
//		}
//		
//	}
}
